import os
import os.path as osp
import cv2
import numpy as np


def main():
    img_path = "evaluation_logs/pred_0418/0_image.png"

    img = cv2.imread(img_path, cv2.IMREAD_GRAYSCALE)

    contours = cv2.findContours(img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
    nums_pre = len(contours)
    print("处理前，共 {} 个！！！".format(nums_pre))

    # 腐蚀处理
    kernel = np.ones((5, 5), dtype=np.uint8)
    erosion_img = cv2.erode(img, kernel, iterations=1)

    erosion_contours = cv2.findContours(erosion_img, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
    nums_post = len(erosion_contours)
    print("处理后，共 {} 个！！！".format(nums_post))

    ss = np.hstack((img, erosion_img))
    cv2.imwrite("ss.png", ss)
    
    return

if __name__ == "__main__":
    main()